package org.friping.mytool.views.picker;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.ViewById;
import org.friping.mytool.R;
import org.friping.mytool.helper.Constants;

@EActivity(R.layout.p_switch)
public class PickerSwitchActivity extends PickerActivity {

    @ViewById
    Switch btnSwitch;

    @ViewById
    TextView labelValueText;

    @ViewById
    TextView labelText;

    @AfterViews
    public void afterView(){
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // render title
        if(title != null){
            actionBar.setTitle(title);
        }else{
            if(label != null){
                actionBar.setTitle("更改"+label);
            }
        }

        // render label
        if(label != null){
            labelText.setText("是否"+label);
        }

        // render value
        btnSwitch.setChecked((Boolean) value);
        processValue((Boolean) value);
    }

    @CheckedChange(R.id.btnSwitch)
    public void change(CompoundButton buttonView, boolean isChecked){
        processValue(isChecked);
    }

    @Click(R.id.btnSave)
    public void save(){
        boolean value = btnSwitch.isChecked();
        Intent intent=new Intent();
        intent.putExtra("name", name);
        intent.putExtra("value", value);
        setResult(Constants.PICKER_SWITCH_RESULT_CODE,intent);
        finish();
    }

    @OptionsItem(android.R.id.home)
    public void optionsItems(MenuItem item) {
        finish();
    }


    private void processValue(boolean value){
        if(Boolean.TRUE.equals(value)){
            labelValueText.setText("是");
        }else{
            labelValueText.setText("否");
        }
    }
}
